package 笔试.面试手撕;

import java.util.Arrays;
import java.util.Stack;

/**
 * @author:谢君臣
 * @Date:2021/8/2-17:14
 * @version:1.0
 * @function:
 */
public class 快排 {
    public static void main(String[] args) {
        int[] a = {14, 7, 16, 5, 3, 22, 8, 10};
        a = QuickSort(a, 0, a.length - 1);
        System.out.println(Arrays.toString(a));
    }

    public static int[] QuickSort(int[] a, int l, int r) {
        if (l < r) {
            int i = l, j = r;
            while (i < j) {
                int temp = a[r];
                while (i < j && a[i] < temp) i++;
                while (i < j && a[j] >= temp) j--;
                if (j > i) swap(a, i, j);
            }
            swap(a, r, i);
            QuickSort(a, l, i - 1);
            QuickSort(a, i + 1, r);
        }
        return a;
    }

    private static void swap(int[] a, int i, int j) {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}